package com.lpb.spring.lpbspring.getway.rtc;

import com.google.common.collect.Maps;
import com.lpb.spring.lpbspring.getway.rtc.enums.EnumServiceSpecification;
import com.lpb.spring.lpbspring.getway.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author linpengbo
 * @date 2023/3/1
 */
public class TrtcUsageVo {

    private List<String> usageKeyList;

    private List<UsageVo> usageList;

    public List<String> getUsageKeyList() {
        return usageKeyList;
    }

    public void setUsageKeyList(List<String> usageKeyList) {
        this.usageKeyList = usageKeyList;
    }

    public List<UsageVo> getUsageList() {
        return usageList;
    }

    public void setUsageList(List<UsageVo> usageList) {
        this.usageList = usageList;
    }


    public static Map<EnumServiceSpecification,Long> filterUsageByTimeInterval(String dateHour,TrtcUsageVo usageVo){
        Map<EnumServiceSpecification,Long> resultMap= Maps.newHashMap();
        if(usageVo==null||CollectionUtils.isEmpty(usageVo.getUsageKeyList()) ||CollectionUtils.isEmpty(usageVo.getUsageList())){
             return  resultMap;
        }
        String date = dateHour.substring(0, 8);
        String hour = dateHour.substring(8, 10);
        String dF = StringUtils.join(date.substring(0,4),"-",date.substring(4,6),"-",date.substring(6,8)," "+hour);
        List<String> usageKeyList = usageVo.getUsageKeyList();
        List<UsageVo> usageList = usageVo.getUsageList();
        for (UsageVo u:usageList) {
           if( u.getTimeKey().indexOf(dF)!=0){
               continue;
           }
            List<Long> usageValueList = u.getUsageValueList();
            for (int i = 0; i <usageValueList.size() ; i++) {
                Long usage = usageValueList.get(i);
                String usageKey = usageKeyList.get(i);
                EnumServiceSpecification enumMark = EnumServiceSpecification.getEnumMark(usageKey);
                if(usage==null||usage.longValue()==0||(enumMark!=EnumServiceSpecification.AUDIO&&enumMark!=EnumServiceSpecification.SD)){
                     continue;
                }
                if(resultMap.containsKey(enumMark)){
                    resultMap.put(enumMark,resultMap.get(enumMark)+usage);
                    continue;
                }
                resultMap.put(enumMark,usage);
            }
        }
        return resultMap;
    }

    public static void main(String[] args) {
        String str="2023-02-27 00:00:00";
        System.out.println(str.indexOf("2023-02-27 00"));
    }

    static class UsageVo{

        private String timeKey;

        private List<Long> usageValueList;

        public String getTimeKey() {
            return timeKey;
        }

        public void setTimeKey(String timeKey) {
            this.timeKey = timeKey;
        }

        public List<Long> getUsageValueList() {
            return usageValueList;
        }

        public void setUsageValueList(List<Long> usageValueList) {
            this.usageValueList = usageValueList;
        }
    }


}
